package edu.gatech.oad.publicstaticdroidmain.taskmanager.model;

import java.util.Date;

public class TaskManagerDBFacade {
   private TaskManager taskManager;
   private DatabaseInterface database;
   
   public TaskManagerDBFacade(TaskManager taskManager, DatabaseInterface database) {
       this.taskManager = taskManager;
       this.database = database;
       
       open();
   }
   
   private void open() {
       database.open();
   }
   
   public void close() {
       database.close();
       taskManager = null;
   }
   
   public void addTask(String name, String description, 
	    Date dueDate, double latitude, double longitude, 
	    Task.Recurrence recurrence, String category) {
       
       Task t = new Task(name, description, dueDate, latitude, longitude, recurrence);
       taskManager.addTask(t, category);
       database.insertTask(t, taskManager.getAccount().getUsername(), category);
   }
   
   public boolean editTask(long id, String name, String description, 
	    Date dueDate, double latitude, double longitude, Task.Recurrence recurrence,
	    boolean completionStatus) {
       boolean successful = taskManager.editTask(id, name, description, dueDate, latitude, longitude, 
	       recurrence, completionStatus);
       
       if (successful) {
	   Task editedTask = new Task(name, description, dueDate, latitude, longitude, recurrence);
	   editedTask.setCompleted(completionStatus);
	   editedTask.setId(id);
	   database.updateTask(editedTask, taskManager.getAccount().getUsername());
       }
       
       return successful;
   }
   
   public boolean removeTask(long id) {
       boolean successful = taskManager.removeTask(id);
       
       if (successful) {
	   database.deleteTask(id);
       }
       
       return successful;
   }
   
   public String[] getTaskData(long id, Task.TaskAttribute[] attributes) { 
	return taskManager.getTaskData(id, attributes);
   }
   
   public String[] getCategories() { 
	return taskManager.getCategories();
   }
   
   public boolean addCategory(String categoryName) { 
	return taskManager.addCategory(categoryName);
   }
   
   public boolean editCategoryName(String oldName, String newName) { 
	return taskManager.editCategoryName(oldName, newName);
   }
   
   public String removeCategory(String categoryName) { 
	return taskManager.removeCategory(categoryName);
   }
   
   public Object[] getMasterListDisplayData() {
	   return taskManager.getMasterListDisplayData();
   }
   
   public String[][] getFilteredListDisplayData(Task.TaskAttribute[] attributes) {
	return taskManager.getFilteredListDisplayData(attributes);
   }
   
   public void filterByCategory(String category) { 
	taskManager.filterByCategory(category);
   }
   
   public void filterByDueDate(Date start, Date end) { 
	taskManager.filterByDueDate(start, end);
   }
   
   public void filterByRecurrence(Task.Recurrence recurrence) { 
	taskManager.filterByRecurrence(recurrence);
   }
   
   public void filterByLatitude(double startLatitude, double endLatitude) { 
	taskManager.filterByLatitude(startLatitude, endLatitude);
   }
   
   public void filterByLongitude(double startLongitude, double endLongitude) { 
	taskManager.filterByLongitude(startLongitude, endLongitude);
   }
   
   public void filterByCompletionStatus(boolean completionStatus) { 
	taskManager.filterByCompletionStatus(completionStatus);
   }
   
   public void searchTaskNames(String text) { 
	taskManager.searchTaskNames(text);
   }
   
   public void searchTaskDescriptions(String text) { 
	taskManager.searchTaskDescriptions(text);
   }
   
   public void resetFilteredList() { 
	taskManager.resetFilteredList();
   }
   
}
